package com.qr.service.impl;

import com.qr.context.ThreadContext;
import com.qr.dto.SuggestDTO;
import com.qr.dto.UserSubmitDTO;
import com.qr.entity.FeedBack;
import com.qr.entity.Move;
import com.qr.entity.UserExercise;
import com.qr.mapper.MoveMapper;
import com.qr.service.MoveService;
import com.qr.vo.NavVO;
import com.qr.vo.TimeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
@Slf4j
public class MoveServiceImpl implements MoveService {

    @Autowired
    private MoveMapper moveMapper;
    public List<NavVO> getNavItem() {
        // 查询所有nav
        List<NavVO> list = moveMapper.getNavItem();
        // 遍历list
        for (NavVO navVO : list) {
            // 查询所有子nav
            List<Move> cardItemList = moveMapper.getCardItem(navVO.getId());
            navVO.setCardItem(cardItemList);
        }
        return list;
    }

    /**
     * 提交数据
     */
    public void submit(List<UserSubmitDTO> submitDTO) {
        LocalDate date = LocalDate.now();
        // 遍历提交的数据
        for (UserSubmitDTO userSubmitDTO : submitDTO) {
            // 合并数据
            UserExercise userExercise = new UserExercise();
            BeanUtils.copyProperties(userSubmitDTO, userExercise);
            log.info("提交的数据为：{}", userSubmitDTO);
            // 获取当前用户ID
            Long userId = ThreadContext.getCurrentId();
            userExercise.setUserId(userId);
            // 先查询在当前时间，有没有同重量，同次数，同动作的数据，如果有，group相加，如果没有插入数据
            List<UserExercise> list = moveMapper.list(userExercise);
            if (list != null && list.size() > 0) {
                // 获取查询到的第一条数据
                UserExercise userExercise1 = list.get(0);
                // 将其group加一
                userExercise1.setGroup(userExercise1.getGroup() + userExercise.getGroup());
                // 并更新
                moveMapper.updateGroupById(userExercise1);
            } else {
                moveMapper.insert(userExercise);
            }
        }
    }

    /**
     * 根据日期获取当天的所有动作
     */
    public List<UserExercise>  getDataByDate(String date) {
        log.info("获取的日期为：{}", date);
        // 获取userId
        Long userId = ThreadContext.getCurrentId();
        // 根据日期查询动作
        List<UserExercise> list = moveMapper.getDataByDate(userId, date);
        log.info("获取的数据为：{}", list);
        return list;
    }

    /**
     * 更新数据
     * @param userExercise
     */
    @Override
    public void updateData(UserExercise userExercise) {
        log.info("更新的数据为：{}", userExercise);
        try {
            // 如果group为0，删除数据
            if (userExercise.getGroup() == 0) {
                moveMapper.delete(userExercise.getId());
                return;
            }
            // 设置更新时间
            userExercise.setUpdateDate(LocalDate.now());
            moveMapper.updateData(userExercise);
        }catch (Exception e){
            log.error("更新数据失败",e);
        }
    }

    /**
     * 获取选择时间内的每个动作的次数
     * @param beginDate
     * @param endDate
     */
    public List<TimeVO> getTimesByDate(LocalDate beginDate, LocalDate endDate) {
        // 获取当前用户ID
        Long userId = ThreadContext.getCurrentId();
        List<TimeVO> timesByDate = moveMapper.getTimesByDate(beginDate, endDate, userId);
        return timesByDate;
    }

    /**
     * 获取选择时间内的对应部位的每个动作的次数
     * @param id
     * @param beginDate
     * @param endDate
     * @return
     */
    public List<TimeVO> getMoveTimesByDate(Integer id, LocalDate beginDate, LocalDate endDate) {
        // 获取当前用户ID
        Long userId = ThreadContext.getCurrentId();
        List<TimeVO> moveTimes = moveMapper.getMoveTimesByDate(id, beginDate, endDate,userId);
        return moveTimes;
    }

    /**
     * 提交建议
     * @param suggestdto
     */
    public void submitSuggest(SuggestDTO suggestdto) {
        // 获取当前用户ID
        Long userId = ThreadContext.getCurrentId();
        FeedBack feedBack = new FeedBack();
        feedBack.setUserId(userId);
        feedBack.setContactInfo(suggestdto.getContact());
        feedBack.setProblemCategory(suggestdto.getCategory());
        feedBack.setProblemDescription(suggestdto.getDescription());
        feedBack.setCreationTime(new Date());
        // 遍历dto的imageURL
        for (String s : suggestdto.getImageURL()) {
            // 判断imageOne是否为空
            if (feedBack.getImageOne() == null) {
                feedBack.setImageOne(s);
            } else if (feedBack.getImageTwo() == null) {
                feedBack.setImageTwo(s);
            } else {
                feedBack.setImageThree(s);
            }
        }
        log.info("提交的建议为：{}", suggestdto);
        moveMapper.insertSuggest(feedBack);
    }

    /**
     * 获取范围内的数据
     * @param startDate
     * @param endDate
     * @return
     */
    public List<String> getDataByDateRange(LocalDate startDate, LocalDate endDate) {
        Long userId = ThreadContext.getCurrentId();
        List<UserExercise> dataByDateRange = moveMapper.getDataByDateRange(startDate, endDate, userId);
        log.info("获取的数据为：{}", dataByDateRange);
        // 新建个set
        Set<String> day = new HashSet<>();
        for (UserExercise userExercise : dataByDateRange) {
            String date = userExercise.getCreateDate().toString();
            String[] split = date.split("-");
            day.add(split[2]);
        }
        // 排序
        List<String> list = new ArrayList<>(day);
        Collections.sort(list);
        return list;
    }
}
